package com.e3.basdoc.employeecls.server;

import java.util.ArrayList;
import java.util.List;

import com.e3.basdoc.employeecls.client.EmployeeClsHelper;
import com.e3.basdoc.employeecls.client.EmployeeClsRPCService;
import com.e3.basdoc.employeecls.model.EmployeeClsModel;
import com.e3.basdoc.employeecls.model.EmployeeClsQC;
import com.e3.core.base.ServiceException;
import com.e3.web.core.client.WebException;
import com.e3.web.core.server.GwtRPCAction;
import com.e3.web.core.server.GwtRPCExecutor;
import com.e3.web.core.server.GwtRPCVoidExecutor;

public class EmployeeClsRPCServiceImpl extends GwtRPCAction implements	EmployeeClsRPCService {

	/**
	 * 
	 */
	public List<EmployeeClsModel> getEmployeeClsChildren(final EmployeeClsModel empclsModel) throws WebException {
		return new GwtRPCExecutor<List<EmployeeClsModel>>() {
			@Override
			public List<EmployeeClsModel> execute() throws ServiceException, Exception {
				List<EmployeeClsModel> models = new ArrayList<EmployeeClsModel>();
				if(empclsModel == null){
					EmployeeClsModel model = EmployeeClsHelper.INSTANCE.createRootModel();
					models.add(model);
					return models;
				}
				
				EmployeeClsModel qModel = empclsModel;
				if(EmployeeClsHelper.INSTANCE.isRootModel(qModel)){
					qModel = null;
				}
				
				models = EmployeeClsServiceClient.getEmployeeClsQueryService().getEmployeeClsChildren(qModel);
				return models;
			}
		}.doExecute();
	}

	public EmployeeClsModel newEmployeeCls(final EmployeeClsModel empclsModel) throws WebException {
		return new GwtRPCExecutor<EmployeeClsModel>() {

			@Override
			public EmployeeClsModel execute() throws ServiceException, Exception {
				return EmployeeClsServiceClient.getEmployeeClsService().newEmployeeCls(empclsModel);
			}

		}.doExecute();
	}

	public EmployeeClsModel modifyEmployeeCls(final EmployeeClsModel empclsModel) throws WebException {
		return new GwtRPCExecutor<EmployeeClsModel>() {

			@Override
			public EmployeeClsModel execute() throws ServiceException, Exception {
				return EmployeeClsServiceClient.getEmployeeClsService().modifyEmployeeCls(empclsModel);
			}

		}.doExecute();
	}

	public List<EmployeeClsModel> getEmployeeClsByCondition(final EmployeeClsQC empclsQC) throws WebException {
		return new GwtRPCExecutor<List<EmployeeClsModel>>() {
			@Override
			public List<EmployeeClsModel> execute() throws ServiceException, Exception {
				List<EmployeeClsModel> empclss = EmployeeClsServiceClient.getEmployeeClsQueryService().getEmployeeClsByCondition(empclsQC);
				if (empclss == null || empclss.isEmpty()) {
					return empclss;
				}
				fillParentEmployeeCls(empclss);
				return empclss;
			}

		}.doExecute();
	}

	protected void fillParentEmployeeCls(List<EmployeeClsModel> empclss) throws Exception{
		for (EmployeeClsModel empclsModel : empclss) {
			if(empclsModel.getParentModel()!=null){
				continue;
			}
			long id = empclsModel.getId();
			if(id == 0){
				continue;
			}
			EmployeeClsModel pModel = EmployeeClsServiceClient.getEmployeeClsQueryService().getEmployeeClsById(id);
			empclsModel.setParentModel(pModel);
		}
	}

	public void deleteEmployeeClses(final List<EmployeeClsModel> empclss) throws WebException {
		new GwtRPCVoidExecutor() {

			@Override
			public void execute() throws ServiceException, Exception {
				if(empclss == null||empclss.isEmpty()){
					return;
				}
				List<Long> ids = new ArrayList<Long>(empclss.size());
				for (EmployeeClsModel empclsModel : empclss) {
					long id = empclsModel.getId();
					ids.add(id);
				}
				EmployeeClsServiceClient.getEmployeeClsService().deleteEmployeeClsByIds(ids);
			}

		}.doExecute();
	}

}
